<?
include "include/config.php";


$user_mod_query=mysql_query("select id,name from users where level=10 ");
$agents["0"]="Alegeti agent";	
while ($users=mysql_fetch_array($user_mod_query)) {
    $agents[$users["id"]] =$users["name"];
}
$smarty->assign('agent',$agents); 
	
$tip=array ('1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$oferta=array ('1'=>'Vanzare','2'=>'Inchiriere');
$valuta=array ('0'=>'Discutabil','1'=>'RON','2'=>'EURO','3'=>'USD'); 

$smarty->assign('tip',$tip);
$smarty->assign('oferta',$oferta);
$smarty->assign('valuta',$valuta);

		
$clauza="";
SmartyPaginate::connect();
if ($_GET['r']!='')
SmartyPaginate::reset();
// set items per page
SmartyPaginate::setLimit(25);
//echo $_REQUEST['pret'];
    
	if (($_REQUEST['locatii_']!='0')AND($_REQUEST['locatii_']!='')) {	 	
		$clauza.=" and (oferta.id_loc='".$_REQUEST['locatii_']."' OR locatii.id_parent='".$_REQUEST['locatii_']."') ";	
		$d1='&locatii_='.$_REQUEST['locatii_'];
	}
	
	if (($_REQUEST['oferta_']!='0')AND($_REQUEST['oferta_']!='')) {
	 	$clauza.=" and oferta.oferta='".$_REQUEST['oferta_']."' ";	
 		$d2='&oferta_='.$_REQUEST['oferta_'];
	}
	
	if (($_REQUEST['tip_']!='0')AND($_REQUEST['tip_']!='')) {
	 	$clauza.=" and oferta.tip='".$_REQUEST['tip_']."' ";	
 		$d3='&tip_='.$_REQUEST['tip_'];
	}
		
	if (($_REQUEST['agent_']!='')AND($_REQUEST['agent_']!='0')) {			
	    $agent_id=$_REQUEST['agent_'];
	    $clauza.=" and oferta.user_id = '{$agent_id}' ";
	    $d4='&agent='.$agent_id;
	}
	
	$clauza_url=$d1.$d2.$d3.$d4;
	
	if ( (($_REQUEST['locatii_']!='0')AND($_REQUEST['locatii_']!=''))OR(($_REQUEST['oferta_']!='0')AND($_REQUEST['oferta_']!=''))OR
	(($_REQUEST['tip_']!='0')AND($_REQUEST['tip_']!=''))OR(($_REQUEST['pret']!='0')AND($_REQUEST['pret']!=''))   )
	SmartyPaginate::setUrl("proces.php"."?".$clauza_url);
	else 
	SmartyPaginate::setUrl("proces.php");
	
	// assign your db results to the template
	$smarty->assign('oferte', get_db_results());
    
	// assign {$paginate} var
	SmartyPaginate::assign($smarty);
	
	function get_db_results() {
	    global $clauza;
	
		
	    $_query = sprintf("select SQL_CALC_FOUND_ROWS oferta.*,locatii.name,parent.name as parent,users.name as user_name, telefon,functie, img, user_id  from oferta
					 left join locatii on oferta.id_loc=locatii.id
					 left join locatii as parent on parent.id=locatii.id_parent
					 inner join users  on oferta.user_id=users.id                                
					 where spof=0 ".$clauza." ORDER BY ID desc LIMIT %d,%d" , SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());
	    $_result = mysql_query($_query);
	     // echo  $_query ; die();
	    list($t_query)=mysql_fetch_array (mysql_query("select FOUND_ROWS()"));
	    while ($cereri_array=mysql_fetch_array($_result))  {	    
	    $img_rows=mysql_fetch_row(mysql_query("select id,src from imagini where id_oferta='".$cereri_array['id']."' "));
	    if ($img_rows["0"]!="") {
		     $cereri_array['src']=$img_rows["1"];
	    }
        
	     list($idp,$numep)=mysql_fetch_array(mysql_query("select L0.id,L0.name
											from locatii L0
											 left join locatii L1 on L1.id_parent=L0.id
                                                                                    where L1.id='".$cereri_array['parentid']."'   ")); 
          
	     if ($idp!='') $cereri_array['parent']=$numep;
        
	     $_data[]=$cereri_array;
	   
	    }	
	    $_result = mysql_query($_query);
	    SmartyPaginate::setTotal($t_query);
	    mysql_free_result($_result);
	    return $_data;
	}   

$smarty->assign("content", $smarty->fetch(tmpd.'proces'.tmpl_ext));
include "include/make_page.php";
?>